package ModelLayer;
import java.util.ArrayList;

/**
 * Container for sale objects
 * 
 * @author (Gruppe 3)
 */

public class SaleContainer
{
    private ArrayList<Sale> sales;
    private static SaleContainer instance;
    
    /**
     * constructor
     */
    private SaleContainer()
    {
        sales = new ArrayList<Sale>();
    }
    
    /**
     * singleton
     */
    public static SaleContainer getInstance()
    {
        if(instance == null)
        {
            instance = new SaleContainer();
        }
        return instance;
    }
    
    /**
     * Adds a sale to the ArrayList sales  
     * @param newSale sale to be added to the ArrayList sales
     */
    public void addSale(Sale newSale)
    {
        if (newSale != null)
        {
            sales.add(newSale);
        }
    }
    
    /**
     * Finds a sale in the ArrayList sales  
     * @param  invoiceNumber the nummer for the sale to search for
     * @return found sale, if it is found, otherwise return null
     */
    public Sale findSale(int invoiceNumber)
    {
        for(int x = 0; x < sales.size(); x++)
        {
            if (sales.get(x).getSaleInvoiceNumber() == invoiceNumber)
            {
                return sales.get(x);
            }
        }
        
        return null;
    }
    
    /**
     * Deletes a sale in the ArrayList sales
     * @param  invoiceNumber the nummer for the sale to delete
     */
    public void deleteSale(int invoiceNumber)
    {
        for(int x = 0; x < sales.size(); x++)
        {
           if (sales.get(x).getSaleInvoiceNumber() == invoiceNumber)
           {
                sales.remove(x); 
           }
        }
    }
    
    /**
     * prints a wanted sale in the ArrayList sales as output
     * all fields in the specific sale is printed if they exist
     * @param  searchInvoiceNumber 
     */
    public void listSale(int searchInvoiceNumber)
    {
        for (int x = 0; x < sales.size(); x++)
        {
            if (sales.get(x).getSaleInvoiceNumber() == searchInvoiceNumber)
            {   
                // print name of the customer
                if (sales.get(x).getCustomer() != null)
                {
                    System.out.println("Kunde: " + sales.get(x).getCustomer().getName());
                }
                
                // print name of the employee
                if (sales.get(x).getEmployee() != null)
                {
                    System.out.println("Ansat: " + sales.get(x).getEmployee().getName());
                }
                
                // lists all bought items in the sale
                System.out.println("Varer købt");            
                sales.get(x).listAllPartSales();
                
                // print total price of the sale
                if (sales.get(x).getTotalPrice() > 0.0)
                {
                    System.out.println("Total pris: " + sales.get(x).getTotalPrice());
                }
                
                // print the buydate
                if (sales.get(x).getDate() != null)
                {
                    System.out.println("Dato: " + sales.get(x).getDate());
                }
                
                // print the invoice number of the sale
                System.out.println("Invoice nummer: " + sales.get(x).getSaleInvoiceNumber());            
                System.out.println("");
            }
        }
    }
           
    
    /**
     *  lists all sales in the ArrayList sales 
     */
    public void listAllSales()
    {   
        // list customer and employee
        for(int x = 0; x < sales.size(); x++)
        {   
            // print name of the customer
            if (sales.get(x).getCustomer() != null)
            {
                System.out.println("Kunde: " + sales.get(x).getCustomer().getName());
            }
            
            // print name of the employee
            if (sales.get(x).getEmployee() != null)
            {
                System.out.println("Ansat: " + sales.get(x).getEmployee().getName());
            }
            
            // lists all bought items in the sale
            System.out.println("Varer købt");            
            sales.get(x).listAllPartSales();
            
            // print total price of the sale
            if (sales.get(x).getTotalPrice() > 0.0)
            {
                System.out.println("Total pris: " + sales.get(x).getTotalPrice());
            }
            
            // print the buydate
            if (sales.get(x).getDate() != null)
            {
                System.out.println("Dato: " + sales.get(x).getDate());
            }
            
            // print the invoice number of the sale
            System.out.println("Invoice nummer: " + sales.get(x).getSaleInvoiceNumber());            
            System.out.println("");
        }
    }
}
